package com.booth.manage.controllers;

import java.util.Date;

import com.booth.base.controllers.BaseController;
import com.booth.base.model.Users;
import com.booth.base.utils.DateUtil;
import com.booth.base.utils.ResultStatus;
import com.booth.manage.model.BoothProgress;
import com.jfinal.plugin.activerecord.Page;

/***
 * 摊位转租控制器
 * @author admin
 *
 */
public class BoothSubletController extends BaseController {
	private boolean success = false;
	
	private final static String BASE_PAGE_URL = "booth_sublet";

	/***
	 * 进入列表页	
	 */
	public void index() {
		render(BASE_PAGE_URL+"_index.jsp");
	}
	
	public void au_page() {
		Integer id = getParaToInt("id");
		setAttr("selectId", id);
		render(BASE_PAGE_URL+"_au.jsp");
	}
	/***
	 * 获取列表页数据
	 */
	public void list() {
		String userName = getPara("userName");
		String boothStatus = getPara("boothStatus");
		String businessStartTime = getPara("businessStartTime");
		String businessEndTime = getPara("businessEndTime");
		Users curUser = super.getCurUser();
		Integer applyId = Integer.MIN_VALUE;
		if(curUser.getGroupType() != null &&  curUser.getGroupType() == GROUP_TYPE_3) {
			applyId = curUser.getUserId();
		}
		int page = getParaToInt("page", 1);
		int pageSize = getParaToInt("rows", 10);
		Page<BoothProgress> pager = BoothProgress.dao.findSubletPage(page, pageSize,userName
				,boothStatus,businessStartTime,businessEndTime,applyId,null);
		setAttr("total", pager.getTotalRow());
		setAttr("rows", pager.getList());
		renderResult(ResultStatus.SUCCESS);
	}
	
	public void doOpenDisable() {
		Integer selectId = getParaToInt("selectId");
		Integer boothStatus = getParaToInt("boothStatus");
		success = BoothProgress.dao.doOpenDisable(selectId,boothStatus, getCurUser());
		if (success){
			renderResult(ResultStatus.SUCCESS);
		}
			
		else
			renderResult(ResultStatus.DELETE_FAILURE);
	}
	
	public void save() {
		Users u = getCurUser();
		BoothProgress item = getModel(BoothProgress.class);
		item.set("sublet_start_time", DateUtil.toDateStr(getParaToDate("sublet_start_time"), DateUtil.YMHMS));
		item.set("sublet_end_time", DateUtil.toDateStr(getParaToDate("sublet_end_time"), DateUtil.YMHMS));
		item.set("sublet_time",new Date());
		setOperator(item, u);
		success = item.update();
		if (success)
			renderResult(ResultStatus.SUCCESS);
		else
			renderResult(ResultStatus.UPDATE_FAILURE);
	}
	/**
	 *  摊位下拉框
	 */
	public void showSubletCombobox()
	{
		String sql = "SELECT user_id as id, concat('名称：[',t.name,']电话：[',t.phone,']') as text FROM buss_opetator t where t.is_delete = 0\n" + 
				"and not EXISTS(\n" + 
				"select 1 from booth_progress a where t.user_id = a.user_id\n" + 
				")";
		renderJson(BoothProgress.dao.find(sql));
	}
	

}
